package com.onesports.intelligent.k12.polarlight.controller.organization.course;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onesports.framework.kit.common.valid.Json;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.BasePageDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.AdjustPriceDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.AdjustPriceRecordDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.CourseOpenDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.CourseQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.Course;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.*;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.course.CourseScheduleTemplateService;
import com.onesports.intelligent.k12.polarlight.service.course.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Wsy
 **/
@RestController("organizationCourseController")
@Api(tags = "机构端-教务管理-课程管理API")
@RequestMapping(ApiPathConstants.ORG_COURSE)
@RequiredArgsConstructor
@PreAuthorize("isAuthenticated()")
public class CourseController {


    private final CourseService courseService;
    private final CourseScheduleTemplateService scheduleTemplateService;

    @GetMapping("/page")
    @ApiOperation("课程分页")
    public ResponseEntity<IPage<CourseVO>> page(@Valid CourseQueryDTO dto) {
        return ResultUtil.success(this.courseService.coursePage(new Page<>(dto.getPage(), dto.getRow()), dto));
    }

    @GetMapping("/sell-price-detail")
    @ApiOperation("查看校区售价详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "row", value = "条数", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "courseId", value = "课程id", required = true, paramType = "query", dataType = "string")
    })
    public ResponseEntity<IPage<CourseSellPriceRelVO>> sellPriceDetail(@NotNull(message = "请选择页数") Long page, @NotNull(message = "请选择每页条数") Long row,
                                                                       @NotBlank(message = "课程id不可为空") String courseId) {
        return ResultUtil.success(this.courseService.sellPriceDetail(new Page<>(page, row), courseId));
    }

    @PostMapping("/apply-adjust-price")
    @ApiOperation("申请调价")
    public ResponseEntity<String> applyAdjustPrice(@Json @RequestBody AdjustPriceDTO dto) {
        this.courseService.applyAdjustPrice(dto);
        return ResultUtil.success("操作成功");
    }


    @GetMapping("/adjust-record")
    @ApiOperation("调价记录")
    public ResponseEntity<IPage<CourseAdjustPriceRecordVO>> adjustRecord(AdjustPriceRecordDTO dto) {
        return ResultUtil.success(this.courseService.adjustRecord(new Page<>(dto.getPage(), dto.getRow()),  null, dto.getCampusId(), dto.getCourseId(), null, null, dto.getTypeName()));
    }


    @PostMapping("/set-open-time")
    @ApiOperation("开设时间设置")
    public ResponseEntity<String> setOpenTime(@Json @RequestBody CourseOpenDTO dto) {
        this.courseService.setOpenTime(dto);
        return ResultUtil.success("操作成功");
    }


    @GetMapping("/course-open")
    @ApiOperation("已设置的开设时间")
    @ApiImplicitParam(name = "courseId", value = "课程id", required = true, paramType = "query", dataType = "string")
    public ResponseEntity<List<CourseOpenVO>> courseOpen(@NotBlank(message = "课程id不可为空") String courseId) {
        return ResultUtil.success(this.courseService.courseOpen(courseId, SecurityUtils.getOrganizationId()));
    }



    @GetMapping("/course-schedule-template")
    @ApiOperation("课程开设时间模板")
    public ResponseEntity<List<CourseScheduleTemplateVO>> courseScheduleTemplate(){
        return ResultUtil.success(this.scheduleTemplateService.courseScheduleTemplate());
    }
}
